package com.example.acceletracks;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;


import com.google.android.gms.maps.model.LatLng;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Handler {

	public static final String KEY_ROWID = "_id";
	public static final String KEY_LAT = "lat";
	public static final String KEY_LNG = "lng";
 
	public static final String DATABASE_NAME = "latlngdb";
	public static final String DATABASE_TABLE = "holes";
	public static final int DATABASE_VERSION = 1;
 
	private DbHelper mkHelper;
	private final Context mkContext;
	private SQLiteDatabase mkDatabase;
	
	
	private static class DbHelper extends SQLiteOpenHelper {
		 
		public DbHelper(Context context){
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			Log.d("db", "Database creating...");
			}

		public void onCreate(SQLiteDatabase db){
			db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_LAT
			+ " DOUBLE NOT NULL, " + KEY_LNG + " DOUBLE NOT NULL);");
			
			Log.d("db", "Creating table" + DATABASE_TABLE + "...");
		}

		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
			}
		}
	
	public Handler(Context c){
		mkContext = c;
	}

	public Handler open() throws SQLException{
		mkHelper = new DbHelper(mkContext);
		mkDatabase = mkHelper.getWritableDatabase();
		return this;
	}

	public void close(){
		if(mkHelper!=null)
		mkHelper.close();
	}
	
		
	public long addItem(LatLng point){
		 
		ContentValues cv = new ContentValues();
		cv.put(KEY_LAT, point.latitude);
		cv.put(KEY_LNG, point.longitude);
		
		Log.d("db", "addItem()" + point.latitude);
		Log.d("db", "addItem()" + point.longitude);
		
		return mkDatabase.insert(DATABASE_TABLE, null, cv);
	}
 
		public HashMap<Integer, LatLng> getMap()
		{
			String[] columns = new String[] { KEY_ROWID, KEY_LAT, KEY_LNG};
			Cursor c = mkDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
			int idIndex = c.getColumnIndex(KEY_ROWID);
			int latIndex = c.getColumnIndex(KEY_LAT);
			int lngIndex = c.getColumnIndex(KEY_LNG);

			HashMap<Integer, LatLng> points = new LinkedHashMap<Integer, LatLng>();

			
			for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
				
				double lat = c.getDouble(latIndex);
				double lng = c.getDouble(lngIndex);
				int index = c.getInt(idIndex);
				
				points.put(index, new LatLng(lat, lng));
			}
			
			return points;
		}
		
		
		public List<LatLng> getList()
		{
			String[] columns = new String[] { KEY_LAT, KEY_LNG};
			Cursor c = mkDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

			int latIndex = c.getColumnIndex(KEY_LAT);
			int lngIndex = c.getColumnIndex(KEY_LNG);

			List<LatLng> points = new LinkedList<LatLng>();

			for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
				
				double lat = c.getDouble(latIndex);
				double lng = c.getDouble(lngIndex);
				
				points.add(new LatLng(lat, lng));
			}
			
			return points;
		}
 
 
		public void delItem(int id) {
			mkDatabase.delete(DATABASE_TABLE, KEY_ROWID + "='"+ id + "'",  null);
			Log.d("db", "delItem()" + KEY_ROWID + "='"+ id + "'");
			
		}
		
		
		public boolean isItem(LatLng point)
		{
			String[] columns = new String[] {KEY_LAT, KEY_LNG};
			String where = KEY_LAT + "='" + point.latitude + "' AND " + KEY_LNG + "='" + point.longitude + "'"; 
			Cursor c = mkDatabase.query(DATABASE_TABLE, columns, where, null, null, null, null);

			//int latIndex = c.getColumnIndex(KEY_LAT);
			//int lngIndex = c.getColumnIndex(KEY_LNG);
			
			Log.d("db", "isItem()::" + c.getCount());
			
			if(c.getCount()!=0)			
			return true;
			else
				return false;
			
		}

		public int getSize() {
			String[] columns = new String[] {KEY_LAT, KEY_LNG};
			
			Cursor c = mkDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
		
			Log.d("db", "getSize()::" + c.getCount());
			return c.getCount();
		}
 

}
